
import { request } from "../../utils/request";
import config from "../../utils/config";
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        picUrl: config.picUrl,
        custom: wx.getMenuButtonBoundingClientRect(),
        user:null,
        bankList:[],
        chooseCard:null,
        cardList:[],
        card_num:'',
        card_bank:'',
        bank_name:'',
        name:'',
        hideChooseCard:true,
        hideAddCard:true,
        hideShadow:true,
        shadowIndex:10,
        amount:'', //提现金额，不能大于可用金额    
    },
    onLoad(options) {
        var timer = setInterval(()=>{
            var user = wx.getStorageSync('userInfo')
            if(user){
                this.setData({user})
                clearInterval(timer)
            }    
        },500)
        wx.hideShareMenu()
        this.getList()
        this.getBankList()
    },
    getList(){
        var user = this.data.user
        if(!user) user = wx.getStorageSync('userInfo')
        request({url:"/miniprogram/user/getCardList",data:{user_id:user.id}}).then(res=>{
            var chooseCard = null
            for(let v of res.data){
                if(v.choose == 1){
                    chooseCard = v
                }
            }
            this.setData({
                cardList:res.data,
                chooseCard:chooseCard
            })
        })
    },
    //打开选择银行卡界面
    chooseCard(){
        this.setData({hideShadow:false,hideChooseCard:false,shadowIndex:10})
    },
    //删除银行卡
    delCard(e){
        var {index,id} = e.currentTarget.dataset
        wx.showModal({
            title:'提示',
            content:'真的要解绑这张银行卡吗？',
            success:res=>{
                if(res.cancel) return false
                request({url:"/miniprogram/user/delCard",data:{id}}).then(res2=>{
                    config.toast(res2.msg,res2.code,res=>{
                        this.getList()
                    })
                })
            }
        })
        
    },
    getCard(e){
        var {index,id} = e.currentTarget.dataset,user_id = this.data.user.id
        this.setData({chooseCard:this.data.cardList[index]})
        this.hideAll()
        request({url:"/miniprogram/user/setCardChoose",data:{id,user_id}}).then(res=>{})
    },
    //添加银行卡
    addCard(){
        this.setData({hideAddCard:false,shadowIndex:12})
    },
    hideAll(){
        this.setData({hideAddCard:true,hideShadow:true,hideChooseCard:true,shadowIndex:10})
    },
    createCard(){
        var {name,card_num,card_bank,bank_name} = this.data,
            user_id = this.data.user.id
        if(!name || !card_num || !card_bank || !bank_name){
            config.toast("持卡人、银行账号、开户行、开户支行必须输入。否则可能无法为您打款",201)
            return false;
        }
        request({
            url:"/miniprogram/user/addCard",
            data:{name,card_num,card_bank,bank_name,user_id},
            type:'post'
        }).then(res=>{
            wx.showToast({title:res.msg,icon:'none'})
            if(res.code != 200) return false
            this.hideAll()
            this.setData({
                chooseCard:{name,card_num,card_bank},
                name:'',
                card_num:'',
                card_bank:''
            })
            this.getList()
        })
    },
    changeValue(e){
        var field = e.currentTarget.dataset.field,
            value = e.detail.value
        this.setData({[field]: value})
    },
    bankChange(e){
        this.setData({card_bank:this.data.bankList[e.detail.value]})
    },
    getBankList(){
        request({url:"/miniprogram/system/getBankList"}).then(res=>{
            this.setData({bankList:res.data})
        })
    },
    set_amount(e){
        var value = e.detail.value
        if(parseFloat(value) > parseFloat(this.data.user.available_balance)){
            this.setData({amount:this.data.user.available_balance})
        }else{
            var v2 = value.split('.')
            if(v2.length > 1 && v2[1].length > 2){
                value = v2[0]+'.'+v2[1].substr(0,2)
            }
            this.setData({amount:value})
        }
    },
    get_amount(){
        var user_id = this.data.user.id,
            amount = parseFloat(this.data.amount),
            name = this.data.chooseCard.name,
            card_num = this.data.chooseCard.card_num,
            card_bank = this.data.chooseCard.card_bank,
            bank_name = this.data.chooseCard.bank_name
        if(!amount || amount <= 0){
            config.toast('提现金额必须输入,且必须大于0。',201,res=>{})
            return false
        }
        if(amount > parseFloat(this.data.user.available_balance)){
            config.toast('提现金额不能大于当前可用余额。',201,res=>{})
            return false;
        }
        if(!name || !card_num || !card_bank || !bank_name){
            config.toast("请先选择提现银行卡。",201,res=>{})
            return false;
        }
        request({
            url:"/miniprogram/user/getMoney",
            data:{user_id,money:amount,name,card_num,card_bank,bank_name},
            type:'post'
        }).then(res=>{
            config.toast(res.msg,res.code,res2=>{
                if(res.code == 200){
                    app.getInfo(user_id)
                    setTimeout(()=>{
                        wx.switchTab({
                          url: '/pages/my/my',
                        })
                    },1500)
                    
                }
            })
        })
    }
})